// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
Titanium.include('functions.js');

// create tab group
var tabGroup = Titanium.UI.createTabGroup();
var deviceWidth  = Titanium.Platform.displayCaps.platformWidth;


// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({  
    title:'Tab 1',
    backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({  
    icon:'KS_nav_views.png',
    title:'Home',
    window:win1
});

//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({  
    title:'Config',
    backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({  
    icon:'KS_nav_ui.png',
    title:'Tab 2',
    window:win2
});



var winPlace = Titanium.UI.createWindow({  
    title:'Places',
    backgroundColor:'#666'
});



///################ DEFINICAO DE WINPLACE ###########
var tableWinPlaces = Titanium.UI.createTableView({
	top: 80,
	left: 10,
	show: 0,
});

winPlace.add(tableWinPlaces);


///################ DEFINICAO DE WIN2 ###########


var fileldName = Titanium.UI.createTextField({
	font:{fontFamily:'Helvetica Neue'},
	top:80,
	value: 'vinicius@open.inf.br',
	width:deviceWidth * 100 / 100,
	title: ' Nome ',
	hintText: ' Nome ',

});

//---------------senha

var fileldSenha = Titanium.UI.createTextField({
	font:{fontFamily:'Helvetica Neue'},
	top:150,
	value: '123456',
	width: deviceWidth * 100 / 100,
	title: ' Senha ',
	hintText: ' Senha ',

	

});

var basicSwitch = Titanium.UI.createSwitch({
    value:true,
    top: 10,
    right: 10
});
basicSwitch.addEventListener('change',function(e)
{
    //Titanium.API.info('Basic Switch value = ' + e.value + ' act val ' + basicSwitch.value);
    if (e.value==1){
    	conecta();
	}
});


win2.add(fileldName);
win2.add(fileldSenha);
win2.add(basicSwitch);






///################ DEFINICAO DE WIN1 ###########


var buttonNFC = Titanium.UI.createButton({
   title: 'Ler NFC',
   top: 10,
	right: 20,
});
buttonNFC.addEventListener('click',function(e)
{
   atualizaPosicaoGPS();
});



var label1 = Titanium.UI.createLabel({
	color:'#999',
	text:'Lugares Próximos',
	font:{fontSize:20,fontFamily:'Helvetica Neue'},
	   top: 50,

	textAlign:'center',
	width:'auto'
});


var tablePlaces = Titanium.UI.createTableView({
	top: 80,
	left: 10,
	show: 0,
});

tablePlaces.addEventListener('click', function(e){
        // event data
        var index = e.index;
        var section = e.section;
        var row = e.row;
        var rowdata = e.rowData;
        //row.hasCheck = true;
        var check = true;
        
       // alert("Index "+ rowdata.id );
        winPlace.open({animate: true,fullscreen: true});
        getServicesOnPlace(rowdata.id );
	}); 


win1.add(tablePlaces);
win1.add(label1);
//win1.add(buttonNFC);








//
//  add tabs
//
tabGroup.addTab(tab1);  
tabGroup.addTab(tab2);  


// open tab group
tabGroup.open();
conecta();
getPlacesAround();